﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Filter;
using COFE.Core.Profile;
using COFE.Core.Utils;

namespace COFE.Core
{
    /// <summary>
    /// Extend IDirectoryInfo.
    /// </summary>
    public static class IDirectoryInfoExAExtension
    {
        /// Return a list of files in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IFileInfo[] GetFiles(this IDirectoryInfo dirEntry, String searchPattern, SearchOptionEx searchOption)
        {

            return new List<IFileInfo>(dirEntry.EnumerateFiles(searchPattern, searchOption)).ToArray();
        }

        /// <summary>
        /// Return a list of files in current directory that match the search filterStr .
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IFileInfo[] GetFiles(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return new List<IFileInfo>(dirEntry.EnumerateFiles(searchPattern)).ToArray();
        }

        /// <summary>
        /// Return a list of files in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IFileInfo[] GetFiles(this IDirectoryInfo dirEntry)
        {
            return new List<IFileInfo>(dirEntry.EnumerateFiles()).ToArray();
        }

        /// <summary>


        /// <summary>
        /// Return a list of directories in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IDirectoryInfo[] GetDirectories(this IDirectoryInfo dirEntry, String searchPattern, SearchOptionEx searchOption)
        {
            return new List<IDirectoryInfo>(dirEntry.EnumerateDirectories(searchPattern, searchOption)).ToArray();
        }


        /// <summary>
        /// Return a list of directories in current directory that match the search filterStr.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IDirectoryInfo[] GetDirectories(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return new List<IDirectoryInfo>(dirEntry.EnumerateDirectories(searchPattern)).ToArray();
        }


        /// <summary>
        /// Return a list of directories in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IDirectoryInfo[] GetDirectories(this IDirectoryInfo dirEntry)
        {
            return new List<IDirectoryInfo>(dirEntry.EnumerateDirectories()).ToArray();
        }


        /// <summary>
        /// Return a list of entries in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IFileSystemInfo[] GetFileSystemInfos(this IDirectoryInfo dirEntry, String searchPattern, SearchOptionEx searchOption)
        {
            return new List<IFileSystemInfo>(dirEntry.EnumerateFileSystemInfos(searchPattern, searchOption)).ToArray();
        }

        /// <summary>
        /// Return a list of entries in current directory that match the search filterStr.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IFileSystemInfo[] GetFileSystemInfos(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return new List<IFileSystemInfo>(dirEntry.EnumerateFileSystemInfos(searchPattern)).ToArray();
        }

        /// <summary>
        /// Return a list of entries in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IFileSystemInfo[] GetFileSystemInfos(this IDirectoryInfo dirEntry)
        {
            return new List<IFileSystemInfo>(dirEntry.EnumerateFileSystemInfos()).ToArray();
        }



        /// <summary>
        /// Return a enumerable of files in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IEnumerable<IFileInfo> EnumerateFiles(this IDirectoryInfo dirEntry, string searchPattern, SearchOptionEx searchOption)
        {
            return dirEntry.InternalProperties.EnumerateFSIs<IFileInfo>(
                PathUtils.MaskToEntryFilter(searchPattern,
                new FileTypeEntryFilter(FileTypeEntryFilter.File)
             ),
             new IOptionFilter[] { new SearchOptionOptionFilter(searchOption) }, EntryConstructionFlags.Default);
        }

        /// <summary>
        /// Return a enumerable of files in current directory that match the search filterStr.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IEnumerable<IFileInfo> EnumerateFiles(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return dirEntry.EnumerateFiles(searchPattern, SearchOptionEx.TopDirectoryOnly);
        }

        /// <summary>
        /// Return a enumerable of files in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IEnumerable<IFileInfo> EnumerateFiles(this IDirectoryInfo dirEntry)
        {
            return dirEntry.EnumerateFiles("*");
        }

        /// <summary>
        /// Return a enumerable of directories in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IEnumerable<IDirectoryInfo> EnumerateDirectories(this IDirectoryInfo dirEntry, string searchPattern, SearchOptionEx searchOption)
        {
            return (dirEntry as IInternalDirectoryInfoExA).EnumerateFSIs<IDirectoryInfo>(
                PathUtils.MaskToEntryFilter(searchPattern,
                new FileTypeEntryFilter(FileTypeEntryFilter.Folder)
             ),
             new IOptionFilter[] { new SearchOptionOptionFilter(searchOption) }, EntryConstructionFlags.Default);
        }

        /// <summary>
        /// Return a enumerable of directories in current directory that match the search filterStr.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IEnumerable<IDirectoryInfo> EnumerateDirectories(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return dirEntry.EnumerateDirectories(searchPattern, SearchOptionEx.TopDirectoryOnly);
        }

        /// <summary>
        /// Return a enumerable of directories in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IEnumerable<IDirectoryInfo> EnumerateDirectories(this IDirectoryInfo dirEntry)
        {
            return dirEntry.EnumerateDirectories("*");
        }


        /// <summary>
        /// Return a enumerable of entries in current directory that match the search filterStr and search options.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(this IDirectoryInfo dirEntry, string searchPattern, SearchOptionEx searchOption)
        {
            return (dirEntry as IInternalDirectoryInfoExA).EnumerateFSIs<IFileSystemInfo>(
                PathUtils.MaskToEntryFilter(searchPattern),               
                new IOptionFilter[] { new SearchOptionOptionFilter(searchOption) },
                EntryConstructionFlags.Default);
        }

        /// <summary>
        /// Return a enumerable of entries in current directory that match the search filterStr.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(this IDirectoryInfo dirEntry, String searchPattern)
        {
            return dirEntry.EnumerateFileSystemInfos(searchPattern, SearchOptionEx.TopDirectoryOnly);
        }

        /// <summary>
        /// Return a enumerable of entries in current directory.
        /// </summary>
        /// <param name="dirEntry"></param>
        /// <returns></returns>
        public static IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(this IDirectoryInfo dirEntry)
        {
            return dirEntry.EnumerateFileSystemInfos("*");
        }







    }
}
